//
//  BCMEditProductViewController.m
//  BCManagerVersion
//
//  Created by ZhaoXiang on 3/8/14.
//  Copyright (c) 2014 Jungle. All rights reserved.
//

#import "BCMEditProductViewController.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "UIViewController+EndWhenTouch.h"
#import "BCMProductTypeItemView.h"
#import "BCProgressHUD.h"


@interface BCMEditProductViewController ()<UITextFieldDelegate, UITextViewDelegate>

@end

@implementation BCMEditProductViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

#define ICON_WIDTH 140
#define ICON_GAP 0

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self addFunctionToEndEdit];
    
    if (self.product.productState == PRODUCT_STATE_NORMAL) {
        self.titleTextField.text = self.product.title;
        self.messageTitleField.text = self.product.message;
    }
    
    self.messageTitleField.layer.borderWidth = 1.0f;
    self.messageTitleField.layer.borderColor = [[UIColor blackColor] CGColor];
    
    
    
    if (self.icons.count > 0) {
        self.iconsScrollView.contentSize = CGSizeMake(self.icons.count * (ICON_GAP + ICON_WIDTH) - ICON_GAP, self.iconsScrollView.frame.size.height);
    }
    
   __block CGRect currentItemFrame = CGRectZero;
    [self.icons enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        
        BCMProductTypeItemView* item = [[BCMProductTypeItemView alloc] initWithFrame:CGRectMake(idx * (ICON_WIDTH + ICON_GAP), 0, ICON_WIDTH, self.iconsScrollView.frame.size.height)];
        
        item.titleLabel.text = obj[@"name"];
        [item.image setImageWithURL:[NSURL URLWithString:obj[@"url"]]];
        
        if ([self.product.image isEqualToString:obj[@"url"]]) {
            currentItemFrame = item.frame;
        }
    
        [self.iconsScrollView addSubview:item];
        
    }];
    
    [self.iconsScrollView scrollRectToVisible:currentItemFrame animated:YES];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)cancel:(id)sender
{
    self.cancel();
}
- (IBAction)publish:(id)sender
{
    CGFloat width = self.iconsScrollView.frame.size.width;
    NSInteger page = (self.iconsScrollView.contentOffset.x + (0.5f * width)) / width;
    [BCProgressHUD showWithTile:@"上传中" animated:YES];
    if (self.product.productState == PRODUCT_STATE_ADD) {
        [BCNetworkClient publishProduct:USER_MANAGER.auth
                                   type:@"add"
                                  title:self.titleTextField.text
                                message:self.messageTitleField.text
                                  image:self.icons[page][@"key"]
                                success:^(NSNumber *productId) {
                                    
                                    [BCProgressHUD hideAnimated:YES];
                                    self.product.productState = PRODUCT_STATE_NORMAL;
                                    self.product.image = self.icons[page][@"url"];
                                    self.product.title = self.titleTextField.text;
                                    self.product.message = self.messageTitleField.text;
                                    self.product.id = productId;
                                    self.success();
                                    
                                } failure:^(NSError *error) {
                                    [BCProgressHUD hideWithError:error animated:YES];
                                    
                                    self.cancel();
                                }];
    } else {
        [BCNetworkClient editProduct:USER_MANAGER.auth
                                type:@"edit"
                           productId:self.product.id
                               title:self.titleTextField.text
                             message:self.messageTitleField.text
                               image:self.icons[page][@"key"]
                             success:^{
                                 [BCProgressHUD hideAnimated:YES];
                                 self.product.image = self.icons[page][@"url"];
                                 self.product.title = self.titleTextField.text;
                                 self.product.message = self.messageTitleField.text;
                                 self.success();
                             } failure:^(NSError *error) {
                                 [BCProgressHUD hideWithError:error animated:YES];
                                 
                                 self.cancel();
                             }];
    }
}

- (instancetype)initWithProduct:(ClientProdutModel*)product
                        success:(VoidBlock)success
                         cancel:(VoidBlock)cancel
{
    self = [self initWithNibName:Nil bundle:nil];
    if (self) {
        self.product = product;
        self.success = success;
        self.cancel = cancel;
    }
    return self;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString* text = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    if (text.length > 20) {
        return NO;
    }
    return YES;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{    
    if ([textView.text stringByReplacingCharactersInRange:range withString:text].length > 140) {
        return NO;
    }
    return YES;
}
@end
